package cn.hloger.spider.demo.carDemo.carDemo;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 正式的
 *
 * @author huanglong
 * @Description TODO
 * @data 2022/9/12
 */
@Service
public class EncryptUtils {

    public static String park_id = "2002312130005";

    public static String AES_KEY = "rq2R9KzrnkvqyFatoU+Zfw==";

    public static String private_key = "MIICXQIBAAKBgQDtRPHiIrzMRpOxTVf1WnIPcb/iXb+Hv1gudtrikHD0OcN1tnDmXOKlY893O1IE2DvxEnB/M8VlGbffdu+dIfpcmu4AghpHGTwLkx6WogXysLHXkPUEvjyOa+R31dJTw5fRYsvEnF0o9K0hYWjrV66BjYw4hv9L6YJUemfxzhSgewIDAQABAoGAXzV9QlriG65ucYUxuHiUsHHap8EqrBQyFGMJLkT7rmTHdCLDZwz6vZcxlwysutjyJKhbkA98NDd/uyIEwK7zwRNzgYzpzurZfeA0Bukc4I4nl1hq02AmqKclCh5IubKlYSSRqOJZIdBeHgzpey2CrX8AGBCOudi9exAy3fyTLPECQQD5XtV4witB3IeryFyJwmTa7lhsAGNtfqVG6a5COnlCGE2YVc0tJrq/VIFZIDWwIcMcXCsGk04jJwEGme5aoJB1AkEA85PAygllhpJ1f9vzI1gJ8N0BkXSfVunJUHUPmB8MLYRiV+V9wIj/ZRDWmCpqKbPZAE87YlWUD/7jaRPx5S5PLwJBAKc1V55pU+gNn0soRiR5HHdQainA+OpqiRhLiHUQEE6pTR5gKfukhIPWfgTs/BLmEJXrl04JZ9NaS+3JOXG7SCECQQCZNu+mJpL+O1I9wxUFvnNCV0KNRFaWTSicu4W7oov+da+GqoyXd5Vu9VCHFucApGhzkWNBT6N7DFHv3uPhzWdxAkAzfH6E/OUa7ZqOHvNmlwrUWkyuO8ton4QTgt09mmxUvITtjCLEfI1XxTTwfplspwsbdzYboXFL29cQlihIcqqV";

    public static String app_id = "202312270001";


    private static AES aes = new AES(Mode.CBC,
            Padding.PKCS5Padding,
            Base64.getDecoder().decode(EncryptUtils.AES_KEY),
            new byte[16]);

    private static Sign sign = new Sign(SignAlgorithm.SHA256withRSA, private_key, null);


    //通用加密
    public static JSONObject encrypt(JSONObject data, LocalDateTime dateTime) {
        JSONObject json = new JSONObject();
        json.put("app_id", app_id);
        json.put("timestamp", DateUtils.getTime(dateTime));
        String dataEncrypt = aesEncrypt(data);
        json.put("data", dataEncrypt);
        json.put("version", "1.0");
        //RAS签名
        String content = getSortContent(json);
        json.put("sign", sign(content));
        return json;
    }

    /**
     * AES加密
     */
    public static String aesEncrypt(JSONObject json) {
        // 加密并进行Base转码
        return aesEncrypt(aes, json);
    }

    public static String aesEncrypt(AES aes, JSONObject json) {
        // 加密并进行Base转码
        return aes.encryptBase64(json.toString(), "UTF-8");
    }

    /**
     * 签名
     */
    public static String sign(String content) {
        return sign(sign, content);
    }

    public static String sign(Sign sign, String content) {
        byte[] encrypt = sign.sign(content);
        return java.util.Base64.getEncoder().encodeToString(encrypt);
    }

    /**
     * 参数排序
     */
    public static String getSortContent(JSONObject data) {
        Map<String, Object> map = data.toJavaObject(Map.class);
        //得到第三方签名 第三方会把sign也放在json里，故转map的时候需要把sign删除
        map.remove("sign");
        List<String> paramKeys = new ArrayList<>(map.keySet());
        Collections.sort(paramKeys);

        StringBuilder sb = new StringBuilder();
        for (String key : paramKeys) {
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(key).append("=").append(map.get(key));
        }
        return sb.toString();
    }
}
